package com.ld.collegetask.fragment;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.ld.collegetask.AboutUsActivity;
import com.ld.collegetask.AddressMemberListAcitivity;
import com.ld.collegetask.MemberEditActivity;
import com.ld.collegetask.MemberIDCardActivity;
import com.ld.collegetask.MemberOrderReceiveListActivity;
import com.ld.collegetask.MemberOrderReleaseListActivity;
import com.ld.collegetask.MemberWalletActivity;
import com.ld.collegetask.R;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.beans.MemberBean;
import com.ld.collegetask.databinding.FragmentMemberBinding;
import com.ld.collegetask.login.LoginActivity;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.SharePreference;

import cn.jpush.im.android.api.JMessageClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import static com.ld.collegetask.R.id.rl_identify;

public class MemberFragment extends BaseFragment implements OnClickListener{
    private static MemberFragment memberFragment;
    private FragmentMemberBinding binding;
    private Activity mActivity;
    private SharePreference sp;

    private Integer userId = 1;

    public MemberFragment() {
    }

    public static MemberFragment getInstance() {
        if (memberFragment == null) {
            memberFragment = new MemberFragment();
        }
        return memberFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_member, container, false);
        sp = new SharePreference(this.getContext());
        userId = sp.getUserId();
        mActivity = getActivity();
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        loading();
    }

    @Override
    public void loading() {
        member_info();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        findView();
    }

    public void findView() {
        binding.rlAddress.setOnClickListener(this);
        binding.memberOrderReleaseListButton.setOnClickListener(this);
        binding.memberOrderReceiveListButton.setOnClickListener(this);
        binding.rlWallet.setOnClickListener(this);
        binding.rlAbout.setOnClickListener(this);
        binding.memberLogoutButton.setOnClickListener(this);
        binding.memberEdit.setOnClickListener(this);
        binding.rlIdentify.setOnClickListener(this);
    }

    private void member_info() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_info(userId);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    MemberBean member = gson.fromJson(base.getJson(), MemberBean.class);
//                    if (member.getAuthentication() == 0){
//                        binding.rlIdentify.setEnabled(true);
//                    }else {
//                        binding.rlIdentify.setEnabled(false);
//                    }
                    binding.setMemberBean(member);
                } else {

                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.member_edit:          //跳转到编辑
                startActivity(new Intent(getActivity(), MemberEditActivity.class).putExtra("ReaName",binding.memberRealname.getText().toString())
                .putExtra("UserImg",binding.getMemberBean().getUserImg()));
                break;
            case R.id.member_order_release_list_button:         //跳转到我的发布
                startActivity(new Intent(getActivity(), MemberOrderReleaseListActivity.class));
                break;
            case R.id.member_order_receive_list_button:          //跳转到我的订单
                startActivity(new Intent(getActivity(), MemberOrderReceiveListActivity.class));
                break;
            case R.id.rl_wallet:                    //跳转到钱包
                startActivity(new Intent(getActivity(), MemberWalletActivity.class));
                break;
            case rl_identify:                   //跳转到身份证认证
                startActivity(new Intent(getActivity(), MemberIDCardActivity.class));
                break;
            case R.id.rl_address:                   //跳转到地址管理
                startActivity(new Intent(getActivity(), AddressMemberListAcitivity.class));
                break;
            case R.id.rl_about:                     //跳转到关于我们
                startActivity(new Intent(getActivity(), AboutUsActivity.class));
                break;
            case R.id.member_logout_button:                //退出登录
                startActivity(new Intent(getActivity(), LoginActivity.class));
                JMessageClient.logout();
                sp.setLoginState(false);
                mActivity.finish();
                break;
        }
    }
}
